<?php
// +----------------------------------------------------------------------
// | 鸿运集团 [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
/**
 * 数据中心
 */
class Member extends Permissions
{
	//会员列表
    public function memlist()
    {
    	$where = [];
        if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['u.start_time'] = ['>=',input('start_time')];
			$where['u.end_time'] = ['<=',input('end_time')];
		}
		if(!empty(input('is_status'))){
			 $where['u.is_status'] = input('is_status');
		}
        if(!empty(input('keywords'))){
			 $where['u.mobile|u.card'] = input('keywords');
		}
		$list = db('user')->alias('u')
		    ->join('userlevel s','u.level_id = s.id','LEFT')
		    ->where($where)
			->order("u.time desc")
			->field('u.*,s.name')
			->paginate(10,false,['query'=>request()->param()]);
		
		//本日新增会员
		$count = db('user')->whereTime('time', 'today')->count();
		//会员总数
		$total = db('user')->count();
		//已激活会员
		$yjh = db('user')->where('is_status',1)->count();
		//待激活会员
		$djh = db('user')->where('is_status',2)->count();
		
		$this->assign('list',$list);
		$this->assign('total',$total);
		$this->assign('count',$count);
		$this->assign('yjh',$yjh);
		$this->assign('djh',$djh);
		$this->assign('is_status',input('is_status'));
		$this->assign('start_time',input('start_time'));
		$this->assign('end_time',input('end_time'));
		$this->assign('keywords',input('keywords'));
        return $this->fetch();
    }

    //编辑会员状态
    public function user_status()
	{
		$data['is_status'] = input('is_status');
		$id = input('uid'); 
		$res = db('user')->where('id',$id)->update($data);
	 	if(input('is_status') == 1){
			$msg = "已激活";
		}else{
			$msg = "已冻结";
		}
	    if($res){
			return json(['code'=>200,'msg'=>$msg]);
        }else{
       		return json(['code'=>400,'msg'=>$msg]);
        }
	}
	
	//编辑会员全部信息
	public function memedit()
	{
		$info = db('user')->where('id',input('id'))->find();
		$this->assign('info',$info); 
		
		//查看所有业务员
		$ywy = db('user')->where('is_ywy',1)->field('user_name,id')->select();
		$this->assign('ywy',$ywy); 
		
		//查看所有会员等级
		$level = db('userlevel')->select();
		$this->assign('level',$level); 
		
		if(request()->isPost()){
			$data = $_POST; 
			if(empty($_POST['password'])){ 
			    unset($data['password']);
		    }else{
		    	$data['password'] = md5_salt($_POST['password']);
		    } 
			if(empty($_POST['balance'])){
			    unset($data['balance']);
		    }else{
		    	$data['balance'] = $info['balance']+$_POST['balance'];
				$data['total_chongzhi'] = $info['total_chongzhi']+$_POST['balance'];//累积充值金额
				$data['total_count'] = $info['total_count']+1;//累积充值笔数
				//打入充值日志
				$datas['type'] = 1;
				$datas['order_sn'] = order_create_no();
				$datas['user_id'] = $_POST['id']; 
				$datas['balance'] = $_POST['balance'];
				$datas['pay_type'] = 4;
				$datas['level_id'] = $_POST['level_id'];
				$datas['time'] = now_time();
				$datas['msg'] = '后台充值';
				db('consume')->insert($datas);
		    }
			$res = db('user')->where('id',$_POST['id'])->update($data);
			if($res){ 
				return $this->success('编辑成功','admin/member/memlist');
	        }else{ 
				return $this->error('编辑失败');
	        }
		}
		return $this->fetch();
	}
	
	//会员详情
	public function meminfo()
	{
		$info = db('user')->alias('u')
		    ->join('userlevel s','u.level_id = s.id','LEFT') 
			->join('user r','r.tj_emp = u.id','LEFT') 
			->field('u.*,s.name,r.user_name as u_name')
			->where('u.id',input('id'))->find();
		$this->assign('info',$info); 
		return $this->fetch();
	}
	
	//充值记录
	public function cz_records()
	{
		$where['c.type'] = 1;
		if(!empty(input('user_name'))){
			 $where['u.user_name'] = input('user_name');
		}
		if(!empty(input('mobile'))){
			 $where['u.mobile'] = input('mobile');
		}
		if(!empty(input('order_sn'))){
			 $where['c.order_sn'] = input('order_sn');
		}
		if(!empty(input('store_id'))){
			 $where['c.store_id'] = input('store_id');
		}
		if(!empty(input('pay_type'))){
			 $where['c.pay_type'] = input('pay_type');
		}
		if(!empty(input('tj_emp'))){
			 $where['u.tj_emp'] = input('tj_emp');
		}
		$data = 'today';
		if(!empty(input('time'))){
			 $data = input('time');
		}
		$list = db('consume')->alias('c')
		    ->join('user u','c.user_id = u.id','LEFT')
			->join('store s','c.store_id = s.id','LEFT')
			->join('user r','r.id = u.tj_emp','LEFT')
			->field("c.*,u.user_name,u.mobile,u.tj_emp,s.store_name,r.user_name as u_name")
		    ->where($where)->whereTime('c.time',$data)
		    ->order("c.time desc")
			->paginate(10,false,['query'=>request()->param()]);
		
		//充值金额
		$total = db('consume')->alias('c')->where('c.type',1)->whereTime('c.time', $data)->sum('balance');
		$this->assign('total',$total);
		//充值笔数
		$count = db('consume')->alias('c')->where('c.type',1)->whereTime('c.time', $data)->count();
		$this->assign('count',$count);
		//赠送卡券
		
		//查看所有业务员
		$ywy = db('user')->where('is_ywy',1)->field('user_name,id')->select();
		$this->assign('ywy',$ywy); 
		//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		$this->assign('user_name',input('user_name'));
		$this->assign('mobile',input('mobile'));
		$this->assign('order_sn',input('order_sn'));
		$this->assign('store_id',input('store_id'));
		$this->assign('time',input('time'));
		$this->assign('pay_type',input('pay_type'));
		$this->assign('tj_emp',input('tj_emp'));
		$this->assign('list',$list);
		return $this->fetch();
	}

    //消费记录
    public function xf_records()
    {
    	$where['c.type'] = 2;
		if(!empty(input('user_name'))){
			 $where['u.user_name'] = input('user_name');
		}
		if(!empty(input('mobile'))){
			 $where['u.mobile'] = input('mobile');
		}
		if(!empty(input('order_sn'))){
			 $where['c.order_sn'] = input('order_sn');
		}
		if(!empty(input('store_id'))){
			 $where['c.store_id'] = input('store_id');
		} 
		$data = 'today';
		if(!empty(input('time'))){
			 $data = input('time');
		}
		$list = db('consume')->alias('c')
		    ->join('user u','c.user_id = u.id','LEFT')
			->join('store s','c.store_id = s.id','LEFT')
			->join('user r','r.id = c.staff_id','LEFT')
			->field("c.*,u.user_name,u.mobile,u.balance as yu_money,s.store_name,r.user_name as u_name")
		    ->where($where)->whereTime('c.time',$data)
		    ->order("c.time desc")
			->paginate(10,false,['query'=>request()->param()]);
    	//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		//累积消费金额
		$total = db('consume')->alias('c')->where('c.type',2)->whereTime('c.time', $data)->sum('balance');
		$this->assign('total',$total);
		//会员总余额
		$yu_money = db('user')->sum('balance');
		$this->assign('yu_money',$yu_money);
		
		$this->assign('user_name',input('user_name'));
		$this->assign('mobile',input('mobile'));
		$this->assign('order_sn',input('order_sn'));
		$this->assign('store_id',input('store_id'));
		$this->assign('time',input('time')); 
		$this->assign('list',$list);
    	return $this->fetch();
    }

    //充值套餐
    public function meal()
	{
		$where = []; 
		if(!empty(input('start_time')) && !empty(input('end_time'))){
			$where['add_time'] = ['between time',[input('start_time'),input('end_time')]];
		}  
		$list = db('fill') 
		    ->where($where)
		    ->order("add_time desc")
			->paginate(10,false,['query'=>request()->param()]); 
		$this->assign('start_time',input('start_time'));
		$this->assign('end_time',input('end_time'));
		$this->assign('list',$list);
		return $this->fetch();
	}

    //新增套餐
    public function mealadd()
	{ 
		if(request()->isPost()){
			$data = $_POST;  
			$data['add_time'] = now_time();
			$res = db('fill')->insert($data);
			if($res){ 
				return $this->success('新增成功','admin/member/meal');
	        }else{ 
				return $this->error('新增失败');
	        }
		}
		return $this->fetch();
	}
	
	//设置套餐
    public function mealedit()
	{ 
		//查询套餐信息
		$info = db('fill')->where('id',input('id'))->find();	
		$this->assign('info',$info);
		if(request()->isPost()){
			$data = $_POST;   
			$res = db('fill')->where('id',$_POST['id'])->update($data);
			if($res){ 
				return $this->success('设置成功','admin/member/meal');
	        }else{ 
				return $this->error('设置失败');
	        }
		}
		return $this->fetch();
	}
	
	//赠送记录
	public function zs_recoreds()
	{
		$where['c.type'] = 3;
		if(!empty(input('user_name'))){
			 $where['u.user_name'] = input('user_name');
		}
		if(!empty(input('mobile'))){
			 $where['u.mobile'] = input('mobile');
		}
		if(!empty(input('order_sn'))){
			 $where['c.order_sn'] = input('order_sn');
		}
		if(!empty(input('store_id'))){
			 $where['c.store_id'] = input('store_id');
		}
		if(!empty(input('pay_type'))){
			 $where['c.pay_type'] = input('pay_type');
		}
		if(!empty(input('tj_emp'))){
			 $where['u.tj_emp'] = input('tj_emp');
		}
		$data = 'today';
		if(!empty(input('time'))){
			 $data = input('time');
		}
		$list = db('consume')->alias('c')
		    ->join('user u','c.user_id = u.id','LEFT')
			->join('store s','c.store_id = s.id','LEFT')
			->join('user r','r.id = u.tj_emp','LEFT')
			->join('user r2','r2.id = c.staff_id','LEFT')
			->field("c.*,u.user_name,u.mobile,u.tj_emp,s.store_name,r.user_name as u_name,r2.user_name as u2_name")
		    ->where($where)->whereTime('c.time',$data)
		    ->order("c.time desc")
			->group('c.order_sn')
			->paginate(10,false,['query'=>request()->param()]);
		 
		//赠送房券
		$total = db('consume')->alias('c')->where('c.type',3)->whereTime('c.time', $data)->sum('zs_zfq');
		$this->assign('total',$total);
		
		//查看所有业务员
		$ywy = db('user')->where('is_ywy',1)->field('user_name,id')->select();
		$this->assign('ywy',$ywy); 
		//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		$this->assign('user_name',input('user_name'));
		$this->assign('mobile',input('mobile'));
		$this->assign('order_sn',input('order_sn'));
		$this->assign('store_id',input('store_id'));
		$this->assign('time',input('time'));
		$this->assign('pay_type',input('pay_type'));
		$this->assign('tj_emp',input('tj_emp'));
		$this->assign('list',$list);
		return $this->fetch();
	}

    //核销
    public function hx_records()
	{
		$where['c.type'] = 4;
		if(!empty(input('user_name'))){
			 $where['u.user_name'] = input('user_name');
		}
		if(!empty(input('mobile'))){
			 $where['u.mobile'] = input('mobile');
		}
		if(!empty(input('order_sn'))){
			 $where['c.order_sn'] = input('order_sn');
		}
		if(!empty(input('store_id'))){
			 $where['c.store_id'] = input('store_id');
		} 
		$data = 'today';
		if(!empty(input('time'))){
			 $data = input('time');
		}
		$list = db('consume')->alias('c')
		    ->join('user u','c.user_id = u.id','LEFT')
			->join('store s','c.store_id = s.id','LEFT')
			->join('user r','r.id = c.staff_id','LEFT')
			->field("c.*,u.user_name,u.mobile,u.staff_id,u.kaquan,s.store_name,r.user_name as u_name")
			->group('c.order_sn')
		    ->where($where)->whereTime('c.time',$data)
		    ->order("c.time desc")
			->paginate(10,false,['query'=>request()->param()]);
		 
		//赠送总房券
		$total = db('consume')->alias('c')->where('c.type',3)->whereTime('c.time', $data)->sum('zs_zfq');
		$this->assign('total',$total);
		//已核销房券
		$num = db('consume')->alias('c')->where('c.type',4)->whereTime('c.time', $data)->sum('num');
		$this->assign('num',$num);
		//未核销房券
		$hx_num = $total-$num;
		$this->assign('hx_num',$hx_num);
		
		//查询所有门店
		$store = db('store')->field('id,store_name')->select();	
		$this->assign('store',$store);
		$this->assign('user_name',input('user_name'));
		$this->assign('mobile',input('mobile'));
		$this->assign('order_sn',input('order_sn'));
		$this->assign('store_id',input('store_id'));
		$this->assign('time',input('time')); 
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	//核销详情
	public function hx_info()
	{ 
		$where['order_sn'] = input('order_sn'); 
		$where['type'] = 4;
		$list = db('consume')
		    ->where($where)->order("time desc")
			->field('room_name,num')
			->paginate(10,false,['query'=>request()->param()]);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	//房券详情
	public function fq_info()
	{
		$where['user_id'] = input('uid');  
		$list = db('userhouse')->alias('u')
	    ->join('room_sort h','u.house_id = h.id','LEFT')
	    ->where($where)
		->field('u.*,h.room_name')
		->paginate(10,false,['query'=>request()->param()]);
		$this->assign('list',$list);
		return $this->fetch(); 
	}

    //会员等级
    public function level()
	{
		$where = [];
		if(!empty(input('name'))){ 
		    $where['u.name'] = input('name');
		}
	    //$total = db('userlevel')->group('name,zs_type')->field('sum(zs_date) as nx,name')->buildSql();
		
		//后续赠送
		$hx = db('levelitem')->where('type',2)->group('level_id')->field('sum(num) as num,level_id')->buildSql();
		//即时赠送
		$js = db('levelitem')->where('type',1)->group('level_id')->field('sum(num) as num1,level_id')->buildSql(); 
		
		$list = db('userlevel')->alias('u')
		    //->join("$total as t",'u.name = t.name','LEFT')
			->join("$hx as h",'h.level_id = u.id','LEFT')
			->join("$js as j",'j.level_id = u.id','LEFT')
		    ->where($where)->order("u.add_time desc") 
			->field('u.*,h.num,j.num1')
			->paginate(10,false,['query'=>request()->param()]);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	//新增会员等级
    public function leveladd()
	{
		//查询所有房型
		$house = db('room_sort')->field('id,room_name')->select();	
		$this->assign('house',$house);
		if(request()->isPost()){
			$result = true;
			Db::startTrans();
			 
			$data['add_time'] = now_time();  
			$data['name'] = $_POST['name'];
			$data['price'] = $_POST['price']; 
			$data['date'] = $_POST['date'];
			$data['zs_date'] = $_POST['zs_date']; //后续赠送年限 
			
			$ids = $_POST['id2'];  //后续赠送
			$ids1 = $_POST['id1'];  //即时赠送
			
            $zs_num = $_POST['zs_num'];//后续房间数量
            $house_id = $_POST['house_id'];//后续房型ID 
            
            $zs_num1 = $_POST['zs_num1'];//即时房间数量
            $house_id1 = $_POST['house_id1'];//即时房型ID 
            if(!empty($zs_num1[0]) && !empty($house_id1[0])){
				$data['zs_date1'] = 1; //即时赠送年限   
			}else{
				$data['zs_date1'] = 0; //即时赠送年限   
			}
			
            //不可以填写重复的等级名称
            $info = db('userlevel')->where('name',$_POST['name'])->find();
			if($info){
				return $this->error('该等级名称已添加，请重新填写');
			}
			//判断即时赠送是否有重复添加一样的房型
			if (count($house_id1) != count(array_unique($house_id1))) {   
			   return $this->error('即时赠送不能重复赠送相同房型');
			}
            //判断后续赠送是否有重复添加一样的房型
			if (count($house_id) != count(array_unique($house_id))) {   
			   return $this->error('后续赠送不能重复赠送相同房型');
			}
			//即时赠送房间总数
			$jishi = array_sum($zs_num1);
			//后续赠送房间总数
			$houxu = array_sum($zs_num)*$_POST['zs_date'];
			 
			//总赠送数量
			$data['zs_num'] = $jishi+$houxu;
			$res = db('userlevel')->insertGetId($data);
			if(!$res) $result = false;
			
			if(!empty($zs_num[0]) && !empty($house_id[0])){
				foreach($ids as $k=>$v){ 
	                $num = $zs_num[$k];
	                $hou =  $house_id[$k];
					$data1['num'] = $num;//后续房间数量
					$data1['house_id'] = $hou; //后续房型ID
					$data1['level_id'] = $res; //等级ID
					$data1['type'] = 2;  //2后续赠送
					//判断房间id和赠送房间数量有值的时候才记录表里
					if(!empty($zs_num[$k]) && !empty($house_id[$k])){
						$res1 = db('levelitem')->insert($data1);
					    if(!$res1) $result = false;
					} 
	            }
			} 
			
			if(!empty($zs_num1[0]) && !empty($house_id1[0])){ 
				foreach($ids1 as $k=>$v){  
					$num1 = $zs_num1[$k];
	                $hou1 =  $house_id1[$k];  
					$data1['num'] = $num1;//即时房间数量
					$data1['house_id'] = $hou1; //即时房型ID
					$data1['level_id'] = $res; //等级ID
					$data1['type'] = 1;//1即时赠送
					if(!empty($zs_num1[$k]) && !empty($house_id1[$k])){
						$res2 = db('levelitem')->insert($data1);
					    if(!$res2) $result = false;
					} 
	            }
			}  
			
			if($result){
				Db::commit();
				return $this->success('新增成功','admin/member/level'); 
			}else{
				Db::rollback();
				return $this->error('新增失败');
			}
          
		}
		return $this->fetch();
	}

    public function levellist()
    {
    	//查询所有房型
		$house = db('room_sort')->field('id,room_name')->select();	
		$this->assign('house',$house);
		$this->assign('type',input('type'));
		echo $this->fetch();
    }
	
	//编辑会员等级
	public function leveledit()
	{
		//查询所有房型
		$house = db('room_sort')->field('id,room_name')->select();	
		$this->assign('house',$house);
		//即时赠送
		$where['level_id'] = input('id');
		$where['type'] = 1;
		$jishi = db('levelitem')->where($where)->field('num,house_id,id')->select();
		$this->assign('jishi',$jishi);
		
		//后续赠送
		$where1['level_id'] = input('id');
		$where1['type'] = 2;
		$houxu = db('levelitem')->where($where1)->field('num,house_id,id')->select();
		$this->assign('houxu',$houxu);
		
		$info = db('userlevel')->where('id',input('id'))->find();
		$this->assign('info',$info);
		
		if(request()->isPost()){
			$result = true;
			Db::startTrans();
			 
			$data['add_time'] = now_time();  
			$data['name'] = $_POST['name'];
			$data['price'] = $_POST['price']; 
			$data['date'] = $_POST['date'];
			$data['zs_date'] = $_POST['zs_date']; //后续赠送年限 
			
			$ids = $_POST['id2'];  //后续赠送
			$ids1 = $_POST['id1'];  //即时赠送
			
            $zs_num = $_POST['zs_num'];//后续房间数量
            $house_id = $_POST['house_id'];//后续房型ID 
            
            $zs_num1 = $_POST['zs_num1'];//即时房间数量
            $house_id1 = $_POST['house_id1'];//即时房型ID   
			//判断即时赠送是否有重复添加一样的房型
			if (count($house_id1) != count(array_unique($house_id1))) {   
			   return $this->error('即时赠送不能重复赠送相同房型');
			}
            //判断后续赠送是否有重复添加一样的房型
			if (count($house_id) != count(array_unique($house_id))) {   
			   return $this->error('后续赠送不能重复赠送相同房型');
			}
			//即时赠送房间总数
			$jishi = array_sum($zs_num1);
			//后续赠送房间总数
			$houxu = array_sum($zs_num)*$_POST['zs_date'];
			 
			//总赠送数量
			$data['zs_num'] = $jishi+$houxu;
			$res = db('userlevel')->where('id',input('ids'))->update($data);
			if(!$res) $result = false;
			
			if(!empty($zs_num[0]) && !empty($house_id[0])){
				foreach($ids as $k=>$v){ 
	                $num = $zs_num[$k];
	                $hou =  $house_id[$k];
					$data1['num'] = $num;//后续房间数量
					$data1['house_id'] = $hou; //后续房型ID
					if($v){
						//编辑
						$res1 = db('levelitem')->where('id',$v)->update($data1); 
					}else{
						//添加
						$data1['level_id'] = input('ids'); //等级ID
						$data1['type'] = 2;  //2后续赠送
						//判断房间id和赠送房间数量有值的时候才记录表里
						if(!empty($zs_num[$k]) && !empty($house_id[$k])){
							$res2 = db('levelitem')->insert($data1);
					        if(!$res2) $result = false;
						} 
					}
	            }
			} 
			
			if(!empty($zs_num1[0]) && !empty($house_id1[0])){ 
				foreach($ids1 as $k=>$v){  
					$num1 = $zs_num1[$k];
	                $hou1 =  $house_id1[$k];  
					$data1['num'] = $num1;//即时房间数量
					$data1['house_id'] = $hou1; //即时房型ID 
					if($v){
						$res3 = db('levelitem')->where('id',$v)->update($data1); 
					}else{
						$data1['level_id'] = input('ids'); //等级ID
						$data1['type'] = 1;//1即时赠送
						//判断房间id和赠送房间数量有值的时候才记录表里
						if(!empty($zs_num1[$k]) && !empty($house_id1[$k])){
							$res4 = db('levelitem')->insert($data1);
					        if(!$res4) $result = false;
						} 
					}
	            }
			}  
			
			if($result){
				Db::commit();
				return $this->success('编辑成功','admin/member/level'); 
			}else{
				Db::rollback();
				return $this->error('编辑失败');
			}
          
		}
		return $this->fetch();
	}

    //会员等级删除
    public function leveldlt()
	{
		$res = db('userlevel')->where('id',input('id'))->delete();
		if($res){
			$res = db('levelitem')->where('level_id',input('id'))->delete();
		}
		if($res){
			return $this->success('删除成功','admin/member/level');
		}else{
			return $this->error('删除失败');
		}
	}
	
	//会员房券编辑
	public function faedit()
	{
		$where['u.user_id'] = input('uid'); 
		$where['u.start_time'] = ['<=',date("Y-m-d")]; 
		$info = db('userhouse')->alias('u')
		    ->join('room_sort h','u.house_id = h.id','LEFT')
			->field('u.*,h.room_name')
		    ->where($where)->select();
		$this->assign('info',$info);
		
		
		if(request()->isPost()){
			$order = order_create_no();
			$time = now_time();
			//查询出，购买会员卡的日志
			$res ['user_id'] = input('user_id');
			$res ['type'] = 1;
			$pay = db('paylog')->where($res)->field('store_id,balance,level_id,staff_id')->find();
			  
			$id = $_POST['id'];
			$num = $_POST['num'];
			foreach($id as $k=>$v){ 
				//查询出之前的数量
				$userhouse = db('userhouse')->where('id',$v)->find(); 
                $num1 = $num[$k]; 
				$data['num'] = $num1+$userhouse['num'];//后续房间数量   
				
				//改变的时候才打入充值日志
				if($num1){
					$res = db('userhouse')->where('id',$v)->update($data); 
					fq_log(3,$order,$userhouse['user_id'],$v,$num1,$time,'卡券变更',array_sum($num),$userhouse['type'],$pay['store_id'],$pay['balance'],$pay['level_id'],$pay['staff_id']);
				}
				
				if($num1){
					//用户表的房券也要累加
				    $res = db('user')->where('id',$userhouse['user_id'])->setInc('kaquan',$num1);
				}
            }
			if($res){
				return $this->success('新增成功','admin/member/memedit');
			}else{
				return $this->error('新增失败');
			}
		}
		return $this->fetch();
	}

    //会员赠送房券详情
	public function user_fq()
	{
		$where['c.order_sn'] = input('order_sn');  
		$list = db('consume')->alias('c')
		    ->join('userhouse s','c.uh_id = s.id','LEFT')
		    ->join('room_sort h','s.house_id = h.id','LEFT')
		    ->where($where)
			->field('c.*,h.room_name,s.start_time,s.end_time')
			->paginate(10,false,['query'=>request()->param()]);
		$this->assign('list',$list);
		return $this->fetch();
	}
}
